﻿/***********************************************************************
 * <copyright file="SqlServerUserDao.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Saturday, March 29, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessSecurity;
using DataObjects;
using DataObjects.ADO;
using DataSecurity.StandartInterfaces;

namespace DataSecurity.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// Defines methods to access User
    /// </summary>
    /// <remarks>
    /// This is a database-independent class. SqlServermplementations are database specific
    /// </remarks>
    public class SqlServerUserDao : IUser
    {
        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>        
        public User GetUser(int userId)
        {
            const string sql = @"uspUser_GetByID";
            object[] parms = { "@UserId", userId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        public List<User> GetUser(string email)
        {
            const string sql = @"uspUser_GetByEmail";
            object[] parms = { "@Email", email };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the users.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<User> GetUsers()
        {
            const string sql = @"uspGetAll_User";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalPage">The total page.</param>
        /// <returns></returns>
        public List<User> GetPaged(int currentPage, ref int totalPage)
        {
            const string sql = @"uspUser_GetPaging";
            object[] parms = { "@CurrentPage", currentPage, "@TotalPage", totalPage };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, out totalPage, parms);
        }

        /// <summary>
        /// Inserts the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object InsertUser(User user)
        {
            const string sql = @"uspInsert_User";
            return Db.Insert(sql, TakeInsert(user));
        }

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object UpdateUser(User user)
        {
            const string sql = @"uspUser_Update";
            return Db.Update(sql, CommandType.StoredProcedure, TakeUpdate(user));
        }

        /// <summary>
        /// Changes the user profile.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public object ChangeUserProfile(User user)
        {
            const string sql = @"uspUpdate_ChangeUserProfile";
            return Db.Update(sql, CommandType.StoredProcedure, TakeChangeUserProfile(user));
        }

        /// <summary>
        /// Deletes the buget source.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object DeleteUser(User user)
        {
            const string sql = @"uspUser_Delete";
            object[] parms = { "@UserId", user.UserId };
            return Db.Update(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Creates a User object based on DataReader.
        /// </summary>
        private static readonly Func<IDataReader, User> Make = reader =>
           new User
           {
               UserId = reader["UserId"].AsId(),
               UserName = reader["UserName"].AsString(),
               IsBlocked = reader["IsBlocked"].AsBool(),
               LastActivityDate = reader["LastActivityDate"].AsDateTime(),
               IsActive = reader["IsActive"].AsBool(),
               FullName = reader["FullName"].AsString(),
               Email = reader["Email"].AsString(),
               Address = reader["Address"].AsString(),
               CreateDate = reader["CreateDate"].AsDateTime(),
               UnitId = reader["UnitID"].AsLong()
           };

        /// <summary>
        /// Creates query parameters list from User object
        /// </summary>
        /// <param name="user"></param>
        /// <returns>Name value parameter list.</returns>
        private static object[] TakeInsert(User user)
        {
            return new object[]  
            {
               "@UserId" , user.UserId,               
               "@UserName" , user.UserName,               
               "@IsBlocked" , user.IsBlocked,
               "@IsActive" , user.IsActive,
               "@Email", user.Email,
               "@FullName", user.FullName,
               "@Address", user.Address,
               "@UnitId", user.UnitId
            };
        }

        /// <summary>
        /// Takes the update.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        private static object[] TakeUpdate(User user)
        {
            return new object[]  
            {
               "@UserId" , user.UserId,
               "@UserName" , user.UserName,   
               "@IsBlocked" , user.IsBlocked,
               "@IsActive" , user.IsActive,
               "@Email", user.Email,
               "@FullName", user.FullName,
               "@Address", user.Address,
               "@UnitId", user.UnitId
            };
        }
        private static object[] TakeChangeUserProfile(User user)
        {
            return new object[]  
            {
               "@UserId" , user.UserId,
               "@Email", user.Email,
               "@FullName", user.FullName,
               "@Address", user.Address,
               "@UnitId", user.UnitId
            };
        }
    }
}
